(function(){

    var i,
        chat = {
        messageToSend: '',
        messageResponses: [
            '您有什么问题，可以根据以下提示，进行服务选择： '+
            '[电脑问题：回复 1]  ' +
            '[打印机问题：回复 2]  ' +
            '[空调问题：回复 3]  ' +
            '[电灯问题：回复 4]  ' +
            '[电梯问题：回复 5]  ',
            '简单的 “电脑” 问题可以查询以下网站，能帮助您自行解决：https://www.sohu.com/a/282606500_395233',
            '简单的 “打印机” 问题可以查询以下网站，能帮助您自行解决：https://www.sohu.com/a/282606500_395233',
            '简单的 “空调” 问题可以查询以下网站，能帮助您自行解决：https://www.sohu.com/a/282606500_395233',
            '简单的 “电灯” 问题可以查询以下网站，能帮助您自行解决：https://www.sohu.com/a/282606500_395233',
            '"电梯" 问题，不建议自行处理，请尽快申报维修或拨打管理员求助电话：800-820-8820',
            '闭嘴,不要种族歧视',
            '你听不懂我说话吗?',
            '...🙄'
        ],
        init: function() {
            this.cacheDOM();
            this.bindEvents();
            this.render();
        },
        cacheDOM: function() {
            this.$chatHistory = $('.chat-history');
            this.$button = $('button');
            this.$textarea = $('#message-to-send');
            this.$chatHistoryList =  this.$chatHistory.find('ul');
        },
        bindEvents: function() {
            this.$button.on('click', this.addMessage.bind(this));
            this.$textarea.on('keyup', this.addMessageEnter.bind(this));
        },
        render: function() {
            this.scrollToBottom();
            if (this.messageToSend.trim() !== '') {
                var template = Handlebars.compile( $("#message-template").html());
                var context = {
                    messageOutput: this.messageToSend,
                    time: this.getCurrentTime()
                };

                this.$chatHistoryList.append(template(context));
                this.scrollToBottom();
                this.$textarea.val('');

                // responses
                var templateResponse = Handlebars.compile( $("#message-response-template").html());
                var contextResponse = {
                    response: this.getRandomItem(this.messageResponses),
                    time: this.getCurrentTime()
                };

                setTimeout(function() {
                    this.$chatHistoryList.append(templateResponse(contextResponse));
                    this.scrollToBottom();
                }.bind(this), 1500);

            }

        },

        addMessage: function() {
            this.messageToSend = this.$textarea.val()
            this.render();
        },
        addMessageEnter: function(event) {
            // enter was pressed
            if (event.keyCode === 13) {
                this.addMessage();
            }
        },
        scrollToBottom: function() {
            this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
        },
        getCurrentTime: function() {
            return new Date().toLocaleTimeString().
            replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
        },
        getRandomItem: function(arr) {
            i = this.messageToSend;
            if(i.includes("negro")){
                return arr[6];
            }

            if(i.includes("你")){
                return arr[7];
            }
            i = Number(i);
            console.log(i);
            if(isNaN(i)){
                return arr[8];
            }

            return arr[i];

        }

    };

    chat.init();

    // var searchFilter = {
    //     options: { valueNames: ['name'] },
    //     init: fun　ction() {
    //         var userList = new List('people-list', this.options);
    //         var noItems = $('<li id="no-items-found">No items found</li>');
    //
    //         userList.on('updated', function(list) {
    //             if (list.matchingItems.length === 0) {
    //                 $(list.list).append(noItems);
    //             } else {
    //                 noItems.detach();
    //             }
    //         });
    //     }
    // };
    //
    // searchFilter.init();

})();